package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsPacking;
import com.jy.wms.pojo.WmsStacking;
import com.jy.wms.service.WmsPackingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsPackingController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author JaneLiu
 * @Date Wed Sep 06 10:34:42 CST 2017
 */
@Controller
@RequestMapping("/wmspacking")
@Api(value = "/wmspacking", description = "关于包装管理操作")
public class WmsPackingController extends BaseController<WmsPackingService,WmsPacking> {

    @PostMapping(value="/list")
    @ApiOperation(value = "包装管理 POST请求查询包装信息",response = JSONObject.class, notes = "POST请求查询包装信息")
    public  @ResponseBody JSONObject getWmsPacking(@RequestBody WmsPacking entity, HttpSession session) {
        Page<WmsPacking> list = new Page<WmsPacking>();
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            entity.setCkid(ckid);
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            if(hz.size()>0){
                entity.setSshzid(hz.get(0).getHzid());
                list=baseService.baseSelectPage(entity);
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    //查货主名称
    @PostMapping(value="/listhzmc")
    @ApiOperation(value = "包装管理 POST请求查询货主名称",response = JSONObject.class, notes = "POST请求查询货主名称")
    public @ResponseBody JSONObject getWmsPackingHZ(HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            String owner=null;//货主id
            String show_owner=null;//货主名称
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                owner=hz.get(0).getHzid();
                show_owner=hz.get(0).getShow_hzid();
                json.put("sshzid",owner);//货主id
                json.put("hzmc",show_owner);//货主名称
            }else{
                json.put("sshzid","");//货主id
                json.put("hzmc","");//货主名称
            }
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(1,"error","货主名称","查看货主名称失败","");
        }
        return json;
    }
     @PostMapping(value = "/add")
     @ApiOperation(value = "包装信息 新增包装信息",response = JSONObject.class, notes = "POST新增包装信息")
     public @ResponseBody JSONObject addWmsPacking(@RequestBody WmsPacking wmsPacking, HttpSession session) {
         JSONObject json = getJSONObject();
         try{
             UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
             String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
             String owner=null;//货主id
             String show_owner=null;//货主名称
             List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
             if(hz.size()>0){
                 owner=hz.get(0).getHzid();
                 show_owner=hz.get(0).getShow_hzid();
             }
             wmsPacking.setSshzid(owner);//货主id
             wmsPacking.setHzmc(show_owner);//货主名称
             WmsPacking packings=baseService.searchAdd(wmsPacking.getBzmc(),ckid,wmsPacking.getSshzid());
             if(packings!=null){
                 json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.info.toString(),"新增包装名称重复","新增包装名称重复","");
             }else {
                 wmsPacking.setId(UUIDUtils.uuid());
                 wmsPacking.setFphpid("0");//分配货品ID 0未分配
                 wmsPacking.setCkid(ckid);
                 baseService.baseInsert(wmsPacking);
             }
         }catch (SucessException e){
             json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"新增包装成功","", JSONObject.fromObject(wmsPacking).toString());
         }catch (ServiceException e){
             e.printStackTrace();
             json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"新增包装失败",e.getMessage(),"");
         }catch (Exception e){
             e.printStackTrace();
             json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增包装失败","新增包装失败","");
         }
        return json;
 }


    @GetMapping(value = "/getWmsPacking/{id}")
    @ApiOperation(value = "包装信息 查看单个包装信息",response = JSONObject.class, notes = "GET查看单个包装信息")
    public @ResponseBody JSONObject getWmsPackingById(@PathVariable("id") String id, HttpSession session) {
        JSONObject json = getJSONObject();
        WmsPacking entity=null;
        try {
            WmsPacking wmsPacking = baseService.baseSelectByPrimaryKey(id);
            json= JSONObject.fromObject(wmsPacking);//传对象
        } catch (Exception e) {
            e.printStackTrace();
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查看单个包装信息失败","查看包装信息失败","");
        }
        return json;
    }

    @PostMapping(value = "/edit")
    @ApiOperation(value = "包装信息 编辑包装信息",response = JSONObject.class, notes = "POST编辑包装信息")
    public  @ResponseBody JSONObject editWmsPacking(@RequestBody WmsPacking wmsPacking, HttpSession session) {
        JSONObject json = getJSONObject();
        Integer  flaga=0;
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            String owner=null;//货主id
            String show_owner=null;//货主名称
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                owner=hz.get(0).getHzid();
                show_owner=hz.get(0).getShow_hzid();//取货主名称
            }
            /*wmsPacking.setFphpid("0");//分配货品ID 0未分配*/
            wmsPacking.setSshzid(owner);//货主id
            wmsPacking.setHzmc(show_owner);//货主名称
            WmsPacking packings=baseService.searchAdds(wmsPacking.getBzmc(),wmsPacking.getId(),ckid,wmsPacking.getSshzid());
            if(packings!=null){
                json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.info.toString(),"编辑包装名称重复","编辑包装名称重复","");
            }else {
                baseService.baseUpdateByPrimaryKeySelectivexg(wmsPacking);
            }
        }catch (SucessException e){
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"编辑包装成功","", JSONObject.fromObject(wmsPacking).toString());

        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"编辑包装失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"编辑包装失败","编辑包装失败","");
        }
        return json;
    }

    /**
     * 静姐的方法
     * 根据货主id查询货主所有供应商
     * @return
     */
    @PostMapping(value = "/getWmsPackingBySshzid")
    @ApiOperation(value = "包装管理 查看单个货主的未绑定货主包装",response = JSONObject.class, notes = "GET查看单个货主的未绑定货主包装")
    public  @ResponseBody JSONObject getWmsPackingBySshzid(@RequestBody Map<String,Object> map, HttpSession session) {
        JSONObject json = getJSONObject();
        List<WmsPacking> entity=  new ArrayList<WmsPacking>();
        SetStatus setStatus = new SetStatus();
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid="";
            if(hz.size()>0){hzid=hz.get(0).getHzid();}
            String mc="";
            if(map.containsKey("mc")){
                mc=map.get("mc").toString();
            }
            entity = baseService.baseSelectBySshzid(mc,hzid);
            json = SetStatus.setSearchType(JSONArray.fromObject(entity).toString(),(int) entity.size());
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
            json = SetStatus.setFunctionType(1,"error","包装管理","查看单个货主的未绑定货主包装失败","");
        }
        return json;
    }

    /**
     * 新增堆码
     * @param wmsStacking
     * @param session
     * @return
     */
    @PostMapping(value = "/addStacking")
    @ApiOperation(value = "堆码信息 新增堆码信息",response = JSONObject.class, notes = "POST新增堆码信息")
    public @ResponseBody JSONObject addStacking(@RequestBody WmsStacking wmsStacking, HttpSession session) {
        JSONObject json = getJSONObject();
        try{
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            String owner=null;//货主id
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                owner=hz.get(0).getHzid();
            }
            wmsStacking.setHzid(owner);//货主id
            wmsStacking.setCkid(ckid);//仓库id
            WmsStacking wmsStackings = baseService.searchStackAdd(wmsStacking.getKwgd(), wmsStacking.getCkid(), wmsStacking.getHzid(), wmsStacking.getBzid());
            if(wmsStackings!=null){
                json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.info.toString(),"新增库位高度重复","新增库位高度重复","");
            }else {
                baseService.insertStacking(wmsStacking);
            }
        }catch (SucessException e){
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"新增堆码成功","", JSONObject.fromObject(wmsStacking).toString());
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"新增堆码失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增堆码失败","新增堆码失败","");
        }
        return json;
    }

    /**
     * 编辑查询堆码
     * @param id
     * @param session
     * @return
     */
    @GetMapping(value = "/getWmsStacking/{id}")
    @ApiOperation(value = "堆码信息 查看单个堆码信息",response = JSONObject.class, notes = "GET查看单个堆码信息")
    public @ResponseBody JSONObject getWmsStacking(@PathVariable("id") String id, HttpSession session) {
        JSONObject json = getJSONObject();
        WmsPacking entity=null;
        try {
            WmsStacking wmsStacking = baseService.getWmsStacking(id);
            json= JSONObject.fromObject(wmsStacking);//传对象
        } catch (Exception e) {
            e.printStackTrace();
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查看单个堆码信息失败","查看堆码信息失败","");
        }
        return json;
    }

    /**
     * 堆码修改保存
     * @param wmsStacking
     * @param session
     * @return
     */
    @PostMapping(value = "/editStacking")
    @ApiOperation(value = "堆码信息 编辑堆码信息",response = JSONObject.class, notes = "POST编辑堆码信息")
    public  @ResponseBody JSONObject editWmsPacking(@RequestBody WmsStacking wmsStacking, HttpSession session) {
        JSONObject json = getJSONObject();
        Integer  flaga=0;
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            String owner=null;//货主id
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                owner=hz.get(0).getHzid();
            }
            wmsStacking.setHzid(owner);//货主id
            wmsStacking.setCkid(ckid);//仓库id
            WmsStacking packings = baseService.searchStackAdds(wmsStacking.getId(), wmsStacking.getBzid(), wmsStacking.getKwgd(), wmsStacking.getCkid(), wmsStacking.getHzid());
            if(packings!=null){
                json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.info.toString(),"编辑堆码库位高度重复","编辑堆码库位高度重复","");
            }else {
                baseService.editStacking(wmsStacking);
            }
        }catch (SucessException e){
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"编辑堆码成功","", JSONObject.fromObject(wmsStacking).toString());

        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"编辑堆码失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"编辑堆码失败","编辑堆码失败","");
        }
        return json;
    }

    /**
     * 删除
     * @param setModel
     * @return
     */
    @PostMapping(value = "/deleteStacking")
    @ApiOperation(value = "堆码信息 多个或单个堆码信息",response = JSONObject.class, notes = "POST删除多个或单个堆码信息")
    public @ResponseBody JSONObject deleteStacking(@RequestBody SetAndModifyModel setModel) {
        JSONObject json = getJSONObject();
        try {
            baseService.deleteStacking(setModel.getIds());
        }catch (SucessException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "删除堆码信息成功", "", "");
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"删除堆码信息失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"删除堆码信息失败","删除堆码信息失败","");
        }
        return json;
    }

    /**
     * 查询堆码列表
     * @param entity
     * @param session
     * @return
     */
    @PostMapping(value="/getListStacking")
    @ApiOperation(value = "堆码管理 POST请求查询堆码信息",response = JSONObject.class, notes = "POST请求查询堆码信息")
    public  @ResponseBody JSONObject getListStacking(@RequestBody WmsStacking entity, HttpSession session) {

        JSONObject json = getJSONObject();
        try {
            Page<WmsStacking> list=baseService.getListStacking(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查询堆码信息失败","查询堆码信息失败","");
        }
        return json;
    }
}
